<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>


    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <style>

    </style>
</head>
<body>

<div id="vuebox">
    <div class="container">
        <div class="row" style="margin-top: 50px;">
            <div class="col-md-6">
                <h1>常见问题检索</h1>
                <form class="form-inline" role="form" style="padding-left: 250px;">
                    <div class="form-group" style="padding-left: 20px;">
                        <label>请输入查询关键字:</label>
                        <select class="form-control" v-model="classId"><!--骆驼命名法-->
                            <option value="0">全部</option>
                            <option v-for="item in classList.data" :value="item.id">{{item.cname}}</option>
                        </select>
                    </div>
                    <div class="form-group" style="padding-left: 20px;">
                        <button type="button" class="btn btn-primary" @click="selectFaqs(1)">查询</button>
                    </div>

                </form>
            </div>
            <div class="col-md-4 text-right">
                <a @click.stop="showModal()" class="btn btn-primary">添加常见问题</a>
            </div>
        </div>

        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>编号</th>
                <th>标题</th>
                <th>分类</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in faqsList.data.list">
                <td>{{item.id}}</td>
                <td>{{item.title}}</td>
                <td>
                    <p v-if="item.classid==1">java</p>
                    <p v-if="item.classid==2">c#</p>
                    <p v-if="item.classid==3">pr</p>
                    <p v-if="item.classid==4">ps</p>
                    <p v-if="item.classid==5">spring</p>
                    <p v-if="item.classid==6">cloud</p>
                </td>
                <td>{{item.createdate}}</td>
                <td>
                    <button class="btn btn-success" @click="to_update_entry(item.id)">修改</button>
                    <button class="btn btn-danger" @click="deleteEntry(item.id)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <br/>
    </div>

    <div class="text-center">
        <ul class="pagination">
            <li v-show="faqsList.data.hasPreviousPage">
                <a href="#" @click.stop="selectFaqs(faqsList.data.perPage)">&laquo;</a>
            </li>
            <li :class="{active:faqsList.data.pageNum==i}" v-for="i of faqsList.data.navigatepageNums">
                <a href="#" @click.stop="selectFaqs(i)">{{i}}</a>
            </li>
            <li v-show="faqsList.data.hasNextPage">
                <a href="#" @click.stop="selectFaqs(faqsList.data.nextPage)">&raquo;</a>
            </li>
        </ul>
    </div>


    <!--    //添加模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" @click="hideModal()">&times;</button>
                    <div class="modal-title">
                        添加常见问题
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">

                        <input type="text" v-model="new_faqs.id" id="faqs_id">

                        <div class="form-group">
                            <label for="classId" class="col-sm-2 control-label">分类</label>
                            <div class="col-sm-10">
                                <select id="classId" class="form-control" v-model="new_faqs.classid">
                                    <option value="0">全部</option>
                                    <option v-for="item in classList.data" :value="item.id">{{item.cname}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="title" class="col-sm-2 control-label">标题</label>
                            <div class="col-sm-10">
                                <input v-model="new_faqs.title" id="title" type="text" class="form-control"
                                       placeholder="请输入图书名称">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="content" class="col-sm-2 control-label">内容</label>
                            <div class="col-sm-10">
                                <input v-model="new_faqs.content" id="content" type="text" class="form-control"
                                       placeholder="请输入上传人">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="createdate" class="col-sm-2 control-label">添加时间</label>
                            <div class="col-sm-10">
                                <input v-model="new_faqs.createdate" id="createdate" type="text" class="form-control"
                                       placeholder="请输入上传时间">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal()">关闭</button>
                    <button class="btn btn-primary" @click="saveFaqs()">保存</button>
                </div>
            </div>
        </div>
    </div>


</div>

</body>
</html>
<script type="text/javascript">
    var vm = new Vue({
        el: "#vuebox",
        data: {
            classList: {},
            classId: "0",
            faqsList: {},
            delResult: {},
            new_faqs: {id: "", classid: "0", title: "", content: "", createdate: ""},
        },
        methods: {
            selectFaqs: function (pageNum) {
                axios.get("/pageInfo", {params: {classId: this.classId, pageNum: pageNum}}).then(res => {
                    this.faqsList = res.data;
                    this.pageNum = pageNum;
                })
            },
            showModal() {
                $("#mymodal").modal("show");
            },
            hideModal() {
                $("#mymodal").modal("hide");
                //清除所有内容
                this.clearFrom();
            },
            clearFrom: function () {
                $("#faqs_id").val("");
                $("#classId").val(0);
                $("#title").val("");
                $("#content").val("");
                $("#createdate").val("");
            },
            saveFaqs: function () {
                axios.post("/add", this.new_faqs).then(res => {
                    this.delResult = res.data;
                    if (this.delResult.data = "SUCCESS") {
                        //增加成功默认第一页
                        this.selectFaqs(this.pageNum);
                        this.hideModal();
                    } else {
                        alert(this.delResult.message);
                    }
                })
            },
            deleteEntry: function (id) {
                axios.get("/delete", {params: {id: id}}).then(res => {
                    this.delResult = res.data;
                    if (this.delResult.result = "SUCCESS") {
                        this.selectFaqs(this.pageNum);
                        alert("删除成功");
                    }else{
                        alert(this.delResult.message);
                    }
                });
            },to_update_entry:function (id) {
                axios.get("/selectById",{params:{id:id}}).then(
                    res=>{this.new_faqs=res.data.data;
                    this.showModal();
                })
            }

        },
        mounted() {
            axios.get("/selectAll").then(res => {
                this.classList = res.data
                this.selectFaqs(1);
            });
        }
    })
</script>